<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入单号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择日期" v-decorator="['date',validatorRules.date]" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-search type="list" v-decorator="['client',validatorRules.client]" :trigger-change="true" dictCode="bas_customer,name,id" @sendNameId="getNameId" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['contact',validatorRules.contact]" placeholder="请输入联系人"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['contactNumber',validatorRules.contactNumber]" placeholder="请输入联系电话"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="交货日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择交货日期" v-decorator="['deliveryDate']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['salesman']" placeholder="请输入业务员"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="摘要" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['digest']" placeholder="请输入摘要"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="23">
                    <a-form-item label="送货地址" :labelCol="{ span: 3 }" :wrapperCol="{ span: 21 }">
                        <a-input v-decorator="['shippingAddress']" placeholder="请输入送货地址"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'

export default {
    name: "SalesOrderModal",
    components: {
        JDate,
    },
    props: ['odd'],
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            dataList: {},
            model: {},
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },

            confirmLoading: false,
            validatorRules: {
                date: {
                    rules: [{
                        required: true,
                        message: '请选择日期!'
                    }]
                },
                client: {
                    rules: [{
                        required: true,
                        message: '请选择客户!'
                    }]
                },
                contact: {
                    rules: [{
                        required: true,
                        message: '请输入联系人!'
                    }]
                },
                contactNumber: {
                    rules: [{
                        required: true,
                        message: '请输入联系电话!'
                    }]
                }
            },
            url: {
                add: "/sales/salesOrder/add",
                edit: "/sales/salesOrder/edit",
            }

        }
    },
    watch: {
        odd() {
            if (this.odd) {
                this.$set(this.dataList, 'odd', this.odd)
            }
        }
    },
    methods: {
        getNameId(value) {
            this.$set(this.dataList, 'clientId', value)
        },
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.dataList.clientId = record.clientId;
            this.odd && (!record.clientId) ? this.$set(this.dataList, 'odd', this.odd) : this.$set(this.dataList, 'odd', record.odd)
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'date', 'client', 'contact', 'contactNumber', 'deliveryDate', 'shippingAddress', 'salesman', 'digest'))
            })
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    values = Object.assign(values, this.dataList);
                    let formData = Object.assign(this.model, values);
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'date', 'client', 'contact', 'contactNumber', 'deliveryDate', 'shippingAddress', 'salesman', 'digest'))
        },

    }
}
</script>
